<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script>
    function runWithTimeout(fn, timeout) {
      return new Promise((resolve, reject) => {
        // 设置超时定时器
        const timeoutId = setTimeout(() => {
          reject(new Error(`操作超时（${timeout}ms）`));
        }, timeout);

        // 执行传入的函数
        Promise.resolve()
          .then(() => fn())
          .then((result) => {
            // 函数成功执行后，清除定时器并返回结果
            clearTimeout(timeoutId);
            resolve(result);
          })
          .catch((error) => {
            // 函数执行出错时，清除定时器并抛出错误
            clearTimeout(timeoutId);
            reject(error);
          });
      });
    }
  </script>
  <body></body>
</html>
